package com.clw.weblogin.controller;

import com.clw.weblogin.common.entity.ResultEntity;
import com.clw.weblogin.common.utils.JsonUtils;
import com.clw.weblogin.entity.JieKuan;
import com.clw.weblogin.entity.JieKuanLiuShui;
import com.clw.weblogin.service.JieKuanLiuShuiService;
import com.clw.weblogin.service.JieKuanService;
import net.sf.json.util.JSONStringer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/jiekuan")
public class JieKuanController {
    @Autowired
    JieKuanService jieKuanService;
    @Autowired
    JieKuanLiuShuiService jieKuanLiuShuiService;

    /**
     * 根据部门id获取数据<p></p>
     * springMVC可将HTTP请求的参数封装到对应实体参数UserEntity对象中，注意请求参数名称和实体类属性名称要相同
     *
     * @param departmentid
     * @return
     */
    @ResponseBody
    @RequestMapping("/select")
    public ResultEntity seledtByDepartment(String departmentid, int page, @RequestParam(required = false, defaultValue = "5") int limit,
                                           String name, String usernumber, String cunkuanmoney, String cunkuandate,String type) throws Exception {
        // start init
        ResultEntity resultEntity = new ResultEntity();
        JieKuan entity = new JieKuan();
        //start end

        //start 实体赋值
        entity.setDepartmentid(departmentid);
        if (name != null) {
            if (name != "" && !name.equals("")) {
                entity.setName(name);
            }
        }
        if (usernumber != null) {
            if (usernumber != "" && !usernumber.equals("")) {
                entity.setUsernumber(usernumber);
            }
        }
        if (cunkuanmoney != null) {
            if (cunkuanmoney != "" && !cunkuanmoney.equals("")) {
                entity.setCunkuanmoney(Double.valueOf(cunkuanmoney));
            }
        }
        if (cunkuandate != null) {
            if (cunkuandate != "" && !cunkuandate.equals("")) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                entity.setCunkuandate(sdf.parse(cunkuandate));
            }
        }
        if(type !=null){
            if (type != "" && !type.equals("")) {
                entity.setType(Integer.valueOf(type));
            }

        }
//        //当前时间+5天。 显示该时间段以后的数据
//        Calendar calendar=Calendar.getInstance();
//        calendar.set(calendar.DATE,calendar.get(Calendar.DATE)+5);
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//        Date date=calendar.getTime();
//        String d=sdf.format(date);
//        entity.setJiesuandate(sdf.parse(d));
        //end实体赋值
        resultEntity = jieKuanService.selectByDepartment(entity, page, limit);
        return resultEntity;
    }

    @ResponseBody
    @RequestMapping("/insert")
    public ResultEntity seledtByDepartment(String name, String usernumber, String book, String phone, String bankcardnumber, String cunkuanmoney, String cunkuandate, String jiesuandate,
                                           String lilv, String cunkuantype, String departmentid, String beizhu, String jiesuanzhouqi, String caozuoren) throws Exception {
        // start init
        ResultEntity resultEntity = new ResultEntity();
        JieKuan jieKuan = new JieKuan();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 数据转换
        try {
            jieKuan.setName(name);
            jieKuan.setBook(book);
            jieKuan.setUsernumber(usernumber);
            jieKuan.setPhone(phone);
            if (bankcardnumber != "") {
                jieKuan.setBankcardnumber(bankcardnumber);
            }
            if (!cunkuanmoney.equals("")) {
                jieKuan.setCunkuanmoney(Double.valueOf(cunkuanmoney));
            }
            if (!cunkuandate.equals("")) {
                jieKuan.setCunkuandate(sdf.parse(cunkuandate));
            }
            jieKuan.setLilv(lilv);
            jieKuan.setCunkuantype(cunkuantype);
//            if (!jiesuandate.equals("")) {
//                jieKuan.setJiesuandate(sdf.parse(jiesuandate));
//            }
            jieKuan.setDepartmentid(departmentid);
            jieKuan.setBeizhu(beizhu);
            if (!jiesuanzhouqi.equals("")) {
                jieKuan.setJiesuanzhouqi(Integer.valueOf(jiesuanzhouqi));
                jieKuan.setJiesuandate(jieKuanService.yujidate(jieKuan.getCunkuandate(), jieKuan.getJiesuanzhouqi()));
            }
            jieKuan.setCaozuoren(caozuoren);
            jieKuan.setType(0);
            //预计利息  一个季度 ||一年 的利息
            if (cunkuanmoney != "" && cunkuandate != "" && lilv != null && jiesuanzhouqi != "") {
//               int days= jieKuanService.days(sdf.parse(cunkuandate),sdf.parse(jiesuandate));
                double doublelilv = Double.valueOf(lilv);
                if (Integer.valueOf(jiesuanzhouqi) != 360) {
                    int value = (int) ((jieKuan.getCunkuanmoney() * doublelilv / 30 * 90) * 100);
                    double monry = value / 100;
                    jieKuan.setYujilixi(monry);
                } else if (Integer.valueOf(jiesuanzhouqi) == 360) {
                    int value = (int) ((jieKuan.getCunkuanmoney() * doublelilv / 360 * 360) * 100);
                    double monry = value / 100;
                    jieKuan.setYujilixi(monry);
                }
                if (jieKuan.getYujilixi() < 0) {
                    jieKuan.setYujilixi(0.00);
                }
            }
            resultEntity = jieKuanService.insert(jieKuan);
            if (resultEntity.getState() == 200) {
                // 向流水表添加 操作记录
                List<JieKuan> list = jieKuanService.selectByExample(jieKuan);
                if (list.size() >= 1) {
                    JieKuanLiuShui liuShui = new JieKuanLiuShui();
                    if (list.get(0).getName() != null) {
                        liuShui.setName(list.get(0).getName());
                    }
                    if (list.get(0).getUsernumber() != null) {
                        liuShui.setUsername(list.get(0).getUsernumber());
                    }
                    if (list.get(0).getBook() != null) {
                        liuShui.setBook(list.get(0).getBook());
                    }
                    if (list.get(0).getPhone() != null) {
                        liuShui.setPhone(list.get(0).getPhone());
                    }
                    if (list.get(0).getBankcardnumber() != null) {
                        liuShui.setBanknumber(list.get(0).getBankcardnumber());
                    }
                    if (list.get(0).getCunkuanmoney() != null) {
                        liuShui.setCunkuanmoney(list.get(0).getCunkuanmoney());
                    }
                    if (list.get(0).getCunkuandate() != null) {
                        liuShui.setCunkuandate(list.get(0).getCunkuandate());
                    }
                    if (list.get(0).getCunkuantype() != null) {
                        liuShui.setCunkuantype(list.get(0).getCunkuantype());
                    }
                    if (list.get(0).getJiesuandate() != null) {
                        liuShui.setJiesuandate(list.get(0).getJiesuandate());
                    }
                    if (list.get(0).getJiesuanzhouqi() != null) {
                        liuShui.setJiesuanzhouqi(list.get(0).getJiesuanzhouqi());
                    }
                    if (list.get(0).getLilv() != null) {
                        liuShui.setLilv(Double.valueOf(list.get(0).getLilv()));
                    }
                    liuShui.setCaozuoleixing("新增数据");
                    if (list.get(0).getDepartmentid() != null) {
                        liuShui.setDepartmentid(list.get(0).getDepartmentid());
                    }
                    if (list.get(0).getBeizhu() != null) {
                        liuShui.setBeizhu(list.get(0).getBeizhu());
                    }
                    if (list.get(0).getCaozuoren() != null) {
                        liuShui.setCaozuoren(list.get(0).getCaozuoren());
                    }
                    liuShui.setType(0);
                    if (list.get(0).getId() != null) {
                        liuShui.setJiekuanid(list.get(0).getId());
                    }
                    liuShui.setCaozuodate(new Date());
                    jieKuanLiuShuiService.insert(liuShui, null);
                }
            }
        } catch (Exception e) {
            resultEntity.setState(100);
            resultEntity.setMessage("数据格式错误");
        }

        return resultEntity;
    }

    @ResponseBody
    @RequestMapping("/update")
    public ResultEntity update(String id, String name, String usernumber, String book, String phone,
                               String bankcardnumber, String cunkuanmoney, String cunkuandate,String jiesuandate,
                               String lilv, String cunkuantype,   String beizhu,  String caozuoren,String departmentid,String jiesuanzhouqi,String enddate,String type) throws Exception {


        // start init
        System.out.println("/jiekuan/update");
        ResultEntity resultEntity = new ResultEntity();
        JieKuan jieKuan = new JieKuan();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 数据转换
        try {
            jieKuan.setId(Integer.valueOf(id));
            jieKuan.setName(name);
            jieKuan.setBook(book);
            jieKuan.setUsernumber(usernumber);
            jieKuan.setPhone(phone);
            jieKuan.setType(Integer.valueOf(type));
            if (enddate!="") {
                jieKuan.setEnddate(sdf.parse(enddate));
            }

            if (bankcardnumber != "") {
                jieKuan.setBankcardnumber(bankcardnumber);
            }
            if (!cunkuanmoney.equals("")) {
                jieKuan.setCunkuanmoney(Double.valueOf(cunkuanmoney));
            }
            if (!cunkuandate.equals("")) {
                jieKuan.setCunkuandate(sdf.parse(cunkuandate));
            }
            jieKuan.setLilv(lilv);
            jieKuan.setCunkuantype(cunkuantype);
            if (!jiesuandate.equals("")) {
                jieKuan.setJiesuandate(sdf.parse(jiesuandate));
            }
            jieKuan.setDepartmentid(departmentid);
            jieKuan.setBeizhu(beizhu);
            if (!jiesuanzhouqi.equals("")) {
                jieKuan.setJiesuanzhouqi(Integer.valueOf(jiesuanzhouqi));
            }
            jieKuan.setCaozuoren(caozuoren);
            jieKuan.setType(0);
            //预计利息  一个季度 ||一年 的利息
            if (cunkuanmoney != "" && cunkuandate != "" && lilv != null && jiesuanzhouqi != "") {
//               int days= jieKuanService.days(sdf.parse(cunkuandate),sdf.parse(jiesuandate));
                double doublelilv = Double.valueOf(lilv);
                if (Integer.valueOf(jiesuanzhouqi) != 360) {
                    int value = (int) ((jieKuan.getCunkuanmoney() * doublelilv / 30 * 90) * 100);
                    double monry = value / 100;
                    jieKuan.setYujilixi(monry);
                } else if (Integer.valueOf(jiesuanzhouqi) == 360) {
                    int value = (int) ((jieKuan.getCunkuanmoney() * doublelilv / 360 * 360) * 100);
                    double monry = value / 100;
                    jieKuan.setYujilixi(monry);
                }
                if (jieKuan.getYujilixi() < 0) {
                    jieKuan.setYujilixi(0.00);
                }
            }
//            //预计利息
//            if (cunkuanmoney != "" && cunkuandate != "" && jiesuandate != "" && lilv != null && jiesuanzhouqi != "") {
//                int days = jieKuanService.days(sdf.parse(cunkuandate), sdf.parse(jiesuandate));
//                double doublelilv = Double.valueOf(lilv);
//                if (Integer.valueOf(jiesuanzhouqi) == 30) {
//                    //  int value=(int)((jieKuan.getCunkuanmoney()* doublelilv / 30 * days)*100);
//                    //    double monry=value/100;
//                    jieKuan.setYujilixi((double) Math.round((jieKuan.getCunkuanmoney() * doublelilv / 30 * days * 100) / 100));
//                } else if (Integer.valueOf(jiesuanzhouqi) == 360) {
//                    //  int value=(int)((jieKuan.getCunkuanmoney()* doublelilv / 360 * days )*100);
//                    //  double monry=value/100;
//                    jieKuan.setYujilixi((double) Math.round((jieKuan.getCunkuanmoney() * doublelilv / 360 * days * 100) / 100));
//                }
//            }
            resultEntity = jieKuanService.update(jieKuan);
            if (resultEntity.getState() == 200) {
                JieKuanLiuShui liuShui = new JieKuanLiuShui();
                if(jieKuan.getId()!=null){
                    liuShui.setJiekuanid(jieKuan.getId());
                }
                if(jieKuan.getName()!=null){
                    liuShui.setName(jieKuan.getName());
                }
                if(jieKuan.getBook()!=null){
                    liuShui.setBook(jieKuan.getBook());
                }
                if(jieKuan.getUsernumber()!=null){
                    liuShui.setUsername(jieKuan.getUsernumber());
                }
                if(jieKuan.getPhone()!=null){
                    liuShui.setPhone(jieKuan.getPhone());
                }
                if(jieKuan.getBankcardnumber()!=null){
                    liuShui.setBanknumber(jieKuan.getBankcardnumber());
                }
                if(jieKuan.getCunkuanmoney()!=null){
                    liuShui.setCunkuanmoney(jieKuan.getCunkuanmoney());
                }
                if(jieKuan.getCunkuandate()!=null){
                    liuShui.setCunkuandate(jieKuan.getCunkuandate());
                }
                if(jieKuan.getLilv()!=null){
                    liuShui.setLilv(Double.valueOf(jieKuan.getLilv()));
                }
                if(jieKuan.getCunkuantype()!=null){
                    liuShui.setCunkuantype(jieKuan.getCunkuantype());
                }
                if(jieKuan.getJiesuandate()!=null){
                    liuShui.setJiesuandate(jieKuan.getJiesuandate());
                }
                if(jieKuan.getDepartmentid()!=null){
                    liuShui.setDepartmentid(jieKuan.getDepartmentid());
                }
                if(jieKuan.getBeizhu()!=null){
                    liuShui.setBeizhu(jieKuan.getBeizhu());
                }
                if(jieKuan.getJiesuanzhouqi()!=null){
                    liuShui.setJiesuanzhouqi(jieKuan.getJiesuanzhouqi());
                }
                if(jieKuan.getCaozuoren()!=null){
                    liuShui.setCaozuoren(jieKuan.getCaozuoren());
                }
                liuShui.setCaozuoleixing("修改");
                liuShui.setCaozuodate(new Date());
                liuShui.setType(0);
                jieKuanLiuShuiService.insert(liuShui, null);
            }
        } catch (Exception e) {
            resultEntity.setState(100);
            resultEntity.setMessage("数据格式错误");
        }

        return resultEntity;
    }

    //删除
    @ResponseBody
    @RequestMapping("/delete")
    public ResultEntity delete(String id,String caozuoren) {
        ResultEntity resultEntity = new ResultEntity();
        JieKuan jieKuan = new JieKuan();
        jieKuan.setId(Integer.valueOf(id));
        jieKuan.setType(2);
        resultEntity = jieKuanService.deleteByid(jieKuan);
        if (resultEntity.getState() == 200) {
            JieKuan en = new JieKuan();
            en.setId(Integer.valueOf(id));
            List<JieKuan> list = jieKuanService.selectByExample(en);
            JieKuanLiuShui liuShui = new JieKuanLiuShui();
            liuShui.setName(list.get(0).getName());
            liuShui.setUsername(list.get(0).getUsernumber());
            liuShui.setBook(list.get(0).getBook());
            liuShui.setPhone(list.get(0).getPhone());
            liuShui.setBanknumber(list.get(0).getBankcardnumber());
            liuShui.setCunkuanmoney(list.get(0).getCunkuanmoney());
            liuShui.setCunkuandate(list.get(0).getCunkuandate());
            liuShui.setCunkuantype(list.get(0).getCunkuantype());
            liuShui.setJiesuandate(list.get(0).getJiesuandate());
            liuShui.setJiesuanzhouqi(list.get(0).getJiesuanzhouqi());
            liuShui.setCaozuoleixing("隐藏数据");
            liuShui.setLilv(Double.valueOf(list.get(0).getLilv()));
            liuShui.setDepartmentid(list.get(0).getDepartmentid());
            liuShui.setBeizhu(list.get(0).getBeizhu());
            liuShui.setCaozuoren(caozuoren);
            liuShui.setType(0);
            liuShui.setCaozuodate(new Date());
            liuShui.setJiekuanid(list.get(0).getId());
            jieKuanLiuShuiService.insert(liuShui, null);
        }
        return resultEntity;
    }

    @ResponseBody
    @RequestMapping("/jiesuan")
    public ResultEntity jiesuan(String id) {
        ResultEntity resultEntity = new ResultEntity();
        JieKuan jieKuan = new JieKuan();
        jieKuan.setId(Integer.valueOf(id));
        jieKuan.setType(1);
        resultEntity = jieKuanService.update(jieKuan);
        return resultEntity;
    }

    // 提醒表 table 数据查询
    @ResponseBody
    @RequestMapping("/selectBydate")
    public ResultEntity selectBydate(String departmentid, String name, String usernumber, String cunkuanmoney, String cunkuandate, int page, @RequestParam(required = false, defaultValue = "7") int limit) throws Exception {
        ResultEntity resultEntity = new ResultEntity();
        JieKuan jieKuan = new JieKuan();
        //据上次结算周期前5天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, + 5);
        //calendar.set(5, calendar.get(5) + 5);
        Date date = calendar.getTime();
        String d = sdf.format(date);
        jieKuan.setJiesuandate(sdf.parse(d));
        if (name != null) {
            if (name != "" && !name.equals("")) {
                jieKuan.setName(name);
            }
        }
        if (usernumber != null) {
            if (usernumber != "" && !usernumber.equals("")) {
                jieKuan.setUsernumber(usernumber);
            }
        }
        if (cunkuanmoney != null) {
            if (cunkuanmoney != "" && !cunkuanmoney.equals("")) {
                jieKuan.setCunkuanmoney(Double.valueOf(cunkuanmoney));
            }
        }
        if (cunkuandate != null) {
            if (cunkuandate != "" && !cunkuandate.equals("")) {
                jieKuan.setCunkuandate(sdf.parse(cunkuandate));
            }
        }

        jieKuan.setDepartmentid(departmentid);
        jieKuan.setType(0);
//        List<JieKuan> list=jieKuanService.selectByExample(jieKuan);
//        List<JieKuan> showlist=new ArrayList<JieKuan>();
//        for (int i =0;i<list.size();i++){
//            int day;
//            if(list.get(i).getEnddate()!=null){
//                 day=jieKuanService.days(list.get(i).getEnddate(),sdf.parse(sdf.format(new Date())));
//            }else{
//                 day=jieKuanService.days(list.get(i).getCunkuandate(),sdf.parse(sdf.format(new Date())));
//            }
//            if(list.get(i).getJiesuanzhouqi()==30){
//                if(day>=85){
//                    showlist.add(list.get(i));
//                }
//            }else if(list.get(i).getJiesuanzhouqi()==360){
//                if(day>=355){
//                    showlist.add(list.get(i));
//                }
//            }
//        }
        resultEntity = jieKuanService.selectByDate(jieKuan, page, limit);
        return resultEntity;
    }
}
